﻿using System.Data.SqlClient;
using System.Linq;
using System.Transactions;
using AB.ServiceDirectory.Commons.DataFilters;
using AB.ServiceDirectory.Entities;

namespace AB.ServiceDirectory.Dao
{
    public sealed class AOrganizationServiceDao
    {
        private static readonly ServicesDirectoryDataContext ServicesDirectoryData = new ServicesDirectoryDataContext();

        private AOrganizationServiceDao()
        {
        }

        /// <summary>
        /// Loads all service organisation.
        /// </summary>
        /// <returns></returns>
        public static IQueryable<ServiceOrganization> LoadAllServiceOrganization()
        {
            IQueryable<ServiceOrganization> query = from s in ServicesDirectoryData.ServiceOrganizations select s;
            return query;
        }


        public static int GetServiceOrganizationCount(AOrganizationServiceFilter filter)
        {
            IQueryable<ServiceOrganization> query = from s in ServicesDirectoryData.ServiceOrganizations select s;
            return FilterOrganizationService(query, filter).Count();
        }

        /// <summary>
        /// Pagings the service organisation service.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <param name="startRowIndex">Start index of the row.</param>
        /// <param name="maxRows">The max rows.</param>
        /// <returns></returns>
        public static IQueryable<ServiceOrganization> PagingServiceOrganizationService(
            AOrganizationServiceFilter filter, int startRowIndex, int maxRows)
        {
            IQueryable<ServiceOrganization> query = from s in ServicesDirectoryData.ServiceOrganizations select s;
            //1. Paging
            query = query.Skip(startRowIndex).Take(maxRows);


            if (filter != null)
            {
                FilterOrganizationService(query, filter);
            }


            return query;
        }


        /// <summary>
        /// Filters the organisation service.
        /// </summary>
        /// <param name="query">The query.</param>
        /// <param name="filter">The filter.</param>
        /// <returns></returns>
        public static IQueryable<ServiceOrganization> FilterOrganizationService(IQueryable<ServiceOrganization> query,
                                                                                AOrganizationServiceFilter filter)
        {
            if (filter.ServiceId != -1)
            {
                query = query.Where(c => c.Organization.IsActive && c.ServiceId == filter.ServiceId);
            }
            return query;
        }

        /// <summary>
        /// Updates the service organization role.
        /// </summary>
        /// <param name="serviceId">The service id.</param>
        /// <param name="organizationId">The organization id.</param>
        /// <param name="roles">The roles.</param>
        /// <returns></returns>
        public static bool UpdateServiceOrganizationRole(int serviceId, int organizationId, string roles)
        {
            ServiceOrganization serviceOrganization =
                ServicesDirectoryData.ServiceOrganizations.FirstOrDefault(
                    s => s.ServiceId == serviceId && s.OrganizationId == organizationId);
            bool result = false;
            try
            {
                using (var transaction = new TransactionScope())
                {
                    if (serviceOrganization != null)
                    {
                        serviceOrganization.Roles = roles;
                        ServicesDirectoryData.SubmitChanges();
                        result = true;
                        transaction.Complete();
                    }
                }
            }
            catch (SqlException e)
            {
                Log.GetLog(e);
                result = false;
            }
            return result;
        }
    }
}